package com.haochang.chunkvod.app.utils;

import com.haochang.chunkvod.model.hostess.ConsumeQueryInfo;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * @author liudong
 *         created at 2016/8/31 10:06
 *         对象排序工具类
 */
public class ComparatorUtil {
    /**
     * 字符串属性比较类
     */
    public static class ComparatorString implements Comparator<ConsumeQueryInfo> {

        private String attr;
        private SortType sortType;

        public ComparatorString(String attr, SortType sortType) {
            this.attr = attr;
            this.sortType = sortType;
        }

        @Override
        public int compare(ConsumeQueryInfo o1, ConsumeQueryInfo o2) {

            if (null != o1 && null != o2) {

                try {
                    Field f1 = o1.getClass().getDeclaredField(attr);
                    Field f2 = o2.getClass().getDeclaredField(attr);
                    f1.setAccessible(true);
                    f2.setAccessible(true);
                    String attr1 = (String) f1.get(o1);
                    String attr2 = (String) f1.get(o2);
                    if (sortType == SortType.DESC) {
                        //按倒序排序，从大到小
                        attr1 = (String) f2.get(o2);
                        attr2 = (String) f1.get(o1);
                    }
                    if (attr1.compareTo(attr2) > 0) {
                        return 1;
                    } else if (attr1.compareTo(attr2) == 0) {
                        return 0;
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
            }
            return -1;
        }
    }

    /**
     * 整型属性比较类
     */
    public static class ComparatorInt implements Comparator<ConsumeQueryInfo> {
        private String attr;
        SortType sortType;

        public ComparatorInt(String attr, SortType sortType) {
            this.attr = attr;
            this.sortType = sortType;
        }

        @Override
        public int compare(ConsumeQueryInfo o1, ConsumeQueryInfo o2) {
            if (null != o1 && null != o2) {
                try {
                    Field f1 = o1.getClass().getDeclaredField(attr);
                    Field f2 = o2.getClass().getDeclaredField(attr);
                    f1.setAccessible(true);
                    f2.setAccessible(true);
                    int attr1 = f1.getInt(o1);
                    int attr2 = f2.getInt(o2);
                    if (sortType == SortType.DESC) {
                        //按倒序排序，从大到小
                        attr1 = f2.getInt(o2);
                        attr2 = f1.getInt(o1);
                    }
                    if (attr1 > attr2) {
                        return 1;
                    } else if (attr1 == attr2) {
                        return 0;
                    }
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            return -1;
        }
    }

    /**
     * 字符串属性排序方法
     *
     * @param list     集合
     * @param attrName 属性名
     * @return
     */
    public static ArrayList<ConsumeQueryInfo> sortByStringAttr(ArrayList<ConsumeQueryInfo> list, String attrName, SortType sortType) {
        Collections.sort(list, new ComparatorString(attrName, sortType));
        return list;
    }

    public static ArrayList<ConsumeQueryInfo> sortByStringAttr(ArrayList<ConsumeQueryInfo> list, String attrName) {

        return sortByStringAttr(list, attrName, SortType.DESC);
    }

    /**
     * 整型属性排序方法
     *
     * @param list     集合
     * @param attrName 属性名
     * @return
     */
    public static ArrayList<ConsumeQueryInfo> sortByIntAttr(ArrayList<ConsumeQueryInfo> list, String attrName, SortType sortType) {
        Collections.sort(list, new ComparatorInt(attrName, sortType));
        return list;
    }

    public static ArrayList<ConsumeQueryInfo> sortByIntAttr(ArrayList<ConsumeQueryInfo> list, String attrName) {
        return sortByIntAttr(list, attrName, SortType.DESC);
    }

    public enum SortType {
        DESC, ASC
    }
}
